// pages/comment/comment.js
const db = wx.cloud.database()
const imgScroll = db.collection('imgScroll')
const userInfo = db.collection('userInfo')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: undefined,
    content: undefined,
    currentview: undefined,
    id: undefined,
    latitude: undefined,
    longitude: undefined, nickName: undefined,
    signature: undefined,
    time: undefined,
    url: undefined,
    currentComment: "",
    comment: []
  },
  // 格式化时间
  getFormatDateTime: function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return [year, '/', month, '/', day, ' ', hour, ':', minute, ':', second].join('');
  },
  onChangCommentContent: function (res) {
    const val = res.detail.value
    this.setData({
      currentComment: val
    })
  },
  // 发送笔记函数
  toSendComment: async function () {
    const { currentComment } = this.data
    if (currentComment.length !== 0) {
      let userInfo = await wx.getStorage({ key: "userInfo" })
      const { avatarUrl, nickName, signature } = userInfo.data
      let locationInfo = await wx.getStorage({ key: "locationInfo" })
      const { latitude, longitude } = locationInfo.data
      const { currentview, id } = this.data
      imgScroll.where({ _id: currentview }).get().then(res => {
        // console.log(res.data[0])
        res.data[0].note[id].comment.unshift({
          avatarUrl, nickName, signature, latitude, longitude, currentview, id, time: this.getFormatDateTime(), content: currentComment
        })
        imgScroll.where({ _id: currentview }).update({
          data: { note: res.data[0].note }
        })
        // console.log(res.data[0].note)
        this.setData({
          comment: res.data[0].note[id].comment,
          currentComment: ""
        })
        // console.log(res.data[0].note[id].comment)
      }).then(() => {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getStorage({
      key: "commentInfo",
      success: (res) => {
        const { avatarUrl, content, currentview, id, latitude, longitude, nickName, signature, time, url } = res.data
        this.setData({ avatarUrl, content, currentview, id, latitude, longitude, nickName, signature, time, url })
        imgScroll.where({ _id: currentview }).get().then(res => {
          const { comment } = res.data[0].note[id]
          // 查看轮播图中是否有评论 没有就新增评论参数
          if (comment) {
            this.setData({ comment })
          } else {
            res.data[0].note[id].comment = []
            imgScroll.where({ _id: currentview }).update({
              data: { note: res.data[0].note }
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})